﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Modules_Tour_TourAE : VST.Controls.BaseLCGPage
{
    public int? TourId { get; set; }
    //public int TourTypeId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        GetPropertyFromQueryString();
        if (!IsPostBack)
        {
            LoadDataParentDDL();
            LoadData();
        }
    }
    void GetPropertyFromQueryString()
    {
        if (Request["TourId"] != null)
            TourId = Request["TourId"].ToInt();
        else
            TourId = null;
    }
    void LoadDataParentDDL()
    {
        List<TblTourTypeEntity> ls = TblTourTypeManager.CreateInstant().SelectTblTourTypeForDDL(-1, WorkingLanguage);
        ddlTourType.DataSource = ls;
        ddlTourType.DataTextField = "Name";
        ddlTourType.DataValueField = "Id";
        ddlTourType.DataBind();
        ddlTourType.Items.Insert(0, new ListItem("------Loại Tour----", "-1"));
    }
    void LoadData()
    {
        if (TourId.HasValue)
        {
            TblTourEntity item = TblTourManager.CreateInstant().SelectOne(TourId.Value);
            txtBeginDate.Text = item.BeginDate;
            imgThumb.ImageUrl = item.ImageTour;
            hdfimgImageTour.Value = item.ImageTour;
            txtMeans.Text = item.Means;
            txtName.Text = item.Name;
            txtPrices.Text = item.Prices.ToString();
            txtPromotionPercent.Text = item.PromotionPercent.ToString();
            txtPromotionPrice.Text = item.PromotionPrice.ToString();
            txtTravelDay.Text = item.TravelDay;
            cbIsHome.Checked = item.IsHome;
            cbIsHot.Checked = item.IsHot;
            cbIsVisible.Checked = item.IsVisible;
            chbIsPromotion.Checked = item.IsPromotion;
            ddlTourType.SelectedValue = item.TourTypeId.ToString();
            LiteralCreateBy.Text = item.CreatedBy;
            txtCreateDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
            lblCreateTime.Text = item.CreatedDate.ToString("HH:mm:ss");
            LiteralUpdatedBy.Text = item.UpdatedBy;
            LiteralUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else
        {
            txtCreateDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            lblCreateTime.Text = DateTime.Now.ToString("HH:mm:ss");
            LiteralUpdatedDate.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        }
    }
    TblTourEntity GetTblTourEntity()
    {
        TblTourEntity a = new TblTourEntity();
        if (TourId.HasValue)
        {
            a = TblTourManager.CreateInstant().SelectOne(TourId.Value);
        }
        else
        {
            a.TextId = TblTourManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text);
            a.DisplayOrder = 1;
            a.CreatedBy = HttpContext.Current.User.Identity.Name;

        }
        if (txtCreateDate.Text.ToDateTime("vi-VN").Date == new DateTime(1900, 1, 1))
        {
            a.CreatedDate = DateTime.Now;
        }
        else
        {
            string[] str = lblCreateTime.Text.Split(new string[] { ":" }, StringSplitOptions.None);
            a.CreatedDate = txtCreateDate.Text.ToDateTime("vi-VN").AddHours(int.Parse(str[0])).AddMinutes(int.Parse(str[1])).AddSeconds(int.Parse(str[2]));
        }
        a.Name = txtName.Text;
        a.TourTypeId = ddlTourType.SelectedValue.ToInt();
        a.TravelDay = txtTravelDay.Text;
        a.BeginDate = txtBeginDate.Text;
        a.DisplayOrder = 1;
        a.ImageTour = hdfimgImageTour.Value;
        a.IsHome = cbIsHome.Checked;
        a.IsHot = cbIsHot.Checked;
        a.IsPromotion = chbIsPromotion.Checked;
        a.IsVisible = cbIsVisible.Checked;
        a.Means = txtMeans.Text;
        a.Prices = txtPrices.Text.ToDecimal();
        a.PromotionPercent = txtPromotionPercent.Text.ToDecimal();
        a.PromotionPrice = txtPromotionPrice.Text.ToDecimal();

        a.UpdatedBy = HttpContext.Current.User.Identity.Name;
        a.UpdatedDate = DateTime.Now;
        return a;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        TblTourManager.CreateInstant().InsertUpdate(GetTblTourEntity());
        TblTourManager.CreateInstant().SortTblTour(ddlTourType.SelectedValue.ToInt());
        Response.Redirect("TourList.aspx");
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And TourTypeId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And TourTypeId='{0}'", strParentId);
        }
        string Whereclause1 = String.Format(" Id='{0}'", strId);
        string Whereclause2 = String.Format(" And Id<>'{0}'", strId);
        Tools.SqlHelper.UpdateOrders("TblTour", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
}